<?php
/**
 * Model to handle contacts
 *
 * @category Abovo
 * 
 * @package Abovo_Model_Contacts
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 */

/**
 * Model to handle contacts
 *
 * @package Abovo
 * 
 * @package Abovo_Model_Contacts
 */
class Abovo_Model_Contacts extends Abovo_Sql_Table {
    
    /**
     * Gender type male
     *
     * @var int
     */
    const GENDER_FEMALE = 0;
    
    /**
     * Gender type female
     *
     * @var int
     */
    const GENDER_MALE = 1;
    
    /**
     * 
     * DataFilter class to use
     * 
     * @var string
     * 
     */
    protected $_filter_class = 'Abovo_DataFilter';
    
    /**
     * 
     * Creates and fetches form hints for contact
     * 
     * @param bool $company Add company form elements or not?
     * 
     * @return Solar_Form
     * 
     */
    public function form($cols = array())
    {
        $form = Solar::factory('Abovo_Form');
        
        settype($cols, 'array');
        
        // if cols is empty then use this set of columns
        if (empty($cols)) {
            $cols = array(
                'companies_id',
                'firstname',
                'lastname',
                'gender',
                'email',
                'tel',
                'adr',
                'postal_code',
                'city',
                'country',
            );
        }
        
        // load form element from table object
        $form->load(
            'Abovo_Form_Load_Table',
            $this,
            $cols,
            'contact'
        );
        
        /*
        // change 'gender' element to select and set options
        $form->elements['contact[gender]']['type']    = 'select';
        $form->elements['contact[gender]']['options'] = array(
            1 => $this->locale('TEXT_GENDER_MALE'),
            0 => $this->locale('TEXT_GENDER_FEMALE'),
        );
        
        // add a company select box
        //
        // remove label
        $form->elements['contact[companies_id]']['label'] = null;
        // change type
        $form->elements['contact[companies_id]']['type'] = 'select';
        $form->elements['contact[companies_id]']['attribs'] = array(
            'style'    => 'width: 250px;',
        );
        
        //$form->elements['contact[postal_code]']['filter'] = array('');
        
        // change country element's type to 'select'
        $form->elements['contact[country]']['type'] = 'select';
        // get list of countrys as options
        $options = Solar::factory('Lux_Intl')->getCountryList();
        $form->elements['contact[country]']['options'] = $options;
        */
        // done.
        return $form;
    }
    
    /**
     * Table schema and indices
     * 
     * @todo indices
     * 
     * @return void
     */
    protected function _setup()
    {
        $this->_name = 'contacts';
        
        $this->_col['companies_id'] = array(
            'type'    => 'int',
        );
        
        // Optional handle is this is info for a user
        $this->_col['handle'] = array(
            'type'    => 'varchar',
            'size'    => 15,
            'require' => true,
            'filter'  => array(
                array('validateRangeLength', 4, 15),
            ),
        );
        
        $this->_col['firstname'] = array(
            'type'    => 'varchar',
            'size'    => 50,
            'require' => true,
        );
        
        $this->_col['lastname'] = array(
            'type'    => 'varchar',
            'size'    => 50,
            'require' => true,
        );
        
        // 0 => 'female', 1 => 'male'
        $this->_col['gender'] = array(
            'type'    => 'bool',
            'require' => true,
        );
        
        // email address
        $this->_col['email'] = array(
            'type'    => 'varchar',
            'size'    => 50,
            'require' => true,
            'filter'   => 'validateEmail',
        );
        
        // home street address
        $this->_col['adr'] = array(
            'type' => 'varchar',
            'size' => 100,
        );
        
        // home city
        $this->_col['city'] = array(
            'type'  => 'varchar',
            'size'  => 50,
        );
        
        // zip code
        $this->_col['postal_code'] = array(
            'type'    => 'varchar',
            'size'    => 5,
            'require' => false,
            'filter'  => 'validateZip',
        );
        
        // home country
        $this->_col['country'] = array(
            'type'    => 'char',
            'size'    => 2,
            'require' => true,
            'default' => Solar::$locale->getCountryCode(),
            'filter'  => 'validateCountry',
        );
        
        // telephone number
        $this->_col['tel'] = array(
            'type'    => 'varchar',
            'size'    => 25,
            'require' => false,
            'filter'  => 'validateTel',
        );
        
        /*
        $this->_col['skype']
        $this->_col['aim']
        $this->_col['msn']
        */
        
        // Make sure sql is available
        if(! Solar::isRegistered('sql')) {
            Solar::register('sql', Solar::factory('Solar_Sql'));
        }
        
    }
}
